package glory.xxd

import org.apache.activemq.ActiveMQConnectionFactory
import org.apache.commons.logging.LogFactory

import javax.jms.*

/**
 *
 * Created by Administrator on 2018-01-23.
 */
class ProducerUtil {


    private static final String USERNAME = "admin";


    private static final String PASSWORD = "admin";

    //正式环境mq
//    private static final String BROKEURL = "tcp://10.10.10.44:61620"
    //测试环境mq
    private static final String BROKEURL = "tcp://192.168.33.240:61624"
    private final static logger = LogFactory.getLog(this)

    /**
     *
     * @param msgId
     * @param msgName
     * @param msgInfo
     * @return
     */
    public static String sendQueueMsg(String msgId, String msgName, String msgInfo) {

        String msg = "0";
        try {
            println "mesinfo:   " + msgInfo
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKEURL);
            Connection connection = connectionFactory.createConnection();
            connection.start();
            Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue(msgName);
            MessageProducer messageProducer = session.createProducer(destination);

            TextMessage message = session.createTextMessage();
            message.setText(msgInfo)
            message.setJMSCorrelationID(msgId)
            //message.setJMSMessageID(msgId)
            messageProducer.send(message);
            session.commit();
            connection.close();
            msg = "1";
//            println "-------------${new Date()}请求Queue:${ProducerUtil.BROKEURL}服务器链接正常!MSGID:${msgId}---------------"
            logger.info("请求Queue:${ProducerUtil.BROKEURL}服务器链接正常!MSGID:${msgId}")
            logger.info(msgInfo)
        } catch (JMSException e) {
            print e
            logger.error("请求Queue:${ProducerUtil.BROKEURL}服务器链接异常!MSGID:${msgId}")
            logger.error(e)
//            println "-------------${new Date()}请求Queue:${ProducerUtil.BROKEURL}服务器链接异常!MSGID:${msgId}---------------"
        }
        return msg;
    }

    /**
     * sendTopicMsg
     * @param msgId
     * @param msgName
     * @param msgInfo
     * @return
     */
    public static String sendTopicMsg(String msgId, String msgName, String msgInfo) {

        String msg = "0";
//        println msgInfo
        Connection connection
        try {
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ProducerUtil.USERNAME, ProducerUtil.PASSWORD, ProducerUtil.BROKEURL);
            connection = connectionFactory.createConnection();
            connection.start();
            Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createTopic(msgName);
            MessageProducer messageProducer = session.createProducer(destination);

            TextMessage message = session.createTextMessage();
            message.setText(msgInfo)
            message.setJMSCorrelationID(msgId)
            //message.setJMSMessageID(msgId)
            messageProducer.send(message);
            session.commit();
            connection.close();
            msg = "1";
//            println "-------------${new Date()}请求Topic:${ProducerUtil.BROKEURL}服务器链接正常!MSGID:${msgId}---------------"
            logger.info("-------------请求Topic:${ProducerUtil.BROKEURL}服务器链接正常!MSGID:${msgId}---------------")
            logger.info(msgInfo)
        } catch (JMSException e) {
//            println "-------------${new Date()}请求Topic:${ProducerUtil.BROKEURL}服务器链接异常!MSGID:${msgId}---------------"
            logger.error("-------------请求Topic:${ProducerUtil.BROKEURL}服务器链接异常!MSGID:${msgId}---------------")
            logger.error(e)
            connection.close();
        }
        return msg;
    }
}
